import unittest
import time

import os
import json
from page.login_page import LoginPage
from tools.utils import chrome
from config.config import DATAS_PATH

cookie_path = os.path.join(DATAS_PATH, 'cookie.txt')
def gen_cookie():
    driver = chrome()
    name = "zhangsan"
    password = "123456"
    lp = LoginPage(driver)
    lp.login(name, password)

    # 获取cookie
    cookies = driver.get_cookies()
    ck = {'name': 'PHPSESSID', 'value': ''}
    for cookie in cookies:
        if cookie['name'] == 'PHPSESSID':
            ck['value'] = cookie['value']
            break

    lp.close()
    with open(cookie_path,'w') as f:
        f.write(json.dumps(ck))

def get_cookie():
    with open(cookie_path,'r') as f:
        cookie = f.read()

    return json.loads(cookie)

if __name__ == '__main__':
    gen_cookie()